﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data;

using DTO;
using Model;

namespace DataService
{
    public class ServiceHelper : BaseService
    {
        public IList<State> GetStates ()
        {
            DataTable table = BaseModel.GetDataTable("GetListState");
            IList<State> list = new List<State>();

            if ( table != null )
            {
                foreach ( DataRow row in table.Rows )
                {
                    list.Add(new State(BaseModel.GetIntFieldValue(row, "AutoId"))
                    {
                        Code = BaseModel.GetStringFieldValue(row, "StateCode"),
                        Name = BaseModel.GetStringFieldValue(row, "StateName")
                    });
                }
            }
            
            return list;
        }
        public IList<Country> GetCountries ()
        {
            DataTable table = BaseModel.GetDataTable("GetListCountry");
            IList<Country> list = new List<Country>();

            if ( table != null )
            {
                foreach ( DataRow row in table.Rows )
                {
                    list.Add(new Country(BaseModel.GetIntFieldValue(row, "AutoId"))
                    {
                        Code = BaseModel.GetStringFieldValue(row, "Code"),
                        Name = BaseModel.GetStringFieldValue(row, "Name")
                    });
                }
            }

            return list;
        }
        public LocationInfo GetLocationById(int id)
        {
            DataTable table = BaseModel.GetDataTable("GetAddressInfoByAddressId", id);

            if (table != null && table.Rows.Count > 0)
            {
                DataRow row = table.Rows[0];
                return new LocationInfo(id)
                {
                    Address = BaseModel.GetStringFieldValue(row, "Address"),
                    Street = BaseModel.GetStringFieldValue(row, "Street2"),
                    City = BaseModel.GetStringFieldValue(row, "City"),
                    State = new State(BaseModel.GetIntFieldValue(row, "StateID"))
                    {
                        Code = BaseModel.GetStringFieldValue(row, "StateCode"),
                        Name = BaseModel.GetStringFieldValue(row, "StateName")
                    },
                    Country = new Country(BaseModel.GetIntFieldValue(row, "CountryID"))
                    {
                        Code = BaseModel.GetStringFieldValue(row, "CountryCode"),
                        Name = BaseModel.GetStringFieldValue(row, "CountryName")
                    },
                    ZipCode = BaseModel.GetStringFieldValue(row, "ZipCode")
               };
            }
            return null;
        }
    }
}
